package com.kaesar.leetcode.LeetCode_351_400;

public class LeetCode_374 extends GuessGame {
    /**
     * 二分查找
     *
     * @param num
     * @return
     */
    public static int guessNumber(int num) {
        int low = 1, high = num;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (guess(mid) == -1) {
                high = mid - 1;
            } else if (guess(mid) == 0) {
                return mid;
            } else if (guess(mid) == 1) {
                low = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(guessNumber(2126753390));
    }
}


class GuessGame {
    /**
     * 假设7是最终的结果
     * Forward declaration of guess API.
     *
     * @param num your guess
     * @return -1 if num is lower than the guess number
     * 1 if num is higher than the guess number
     * otherwise return 0
     */
    public static int guess(int num) {
        if (num > 1702766719) {
            return -1;
        } else if (num == 1702766719) {
            return 0;
        } else {
            return 1;
        }
    }
}